package com.pro.common.modules.service.dependencies.service;

import com.pro.common.api.cursor.sync.service.ICursorSyncHandler;
import com.pro.common.api.service.ITestService;
import com.pro.common.cursor.sync.service.CursorSyncExecutor;
import com.pro.framework.lock.redisson.RedissonLockExecutor;
import com.xxl.job.core.biz.model.ReturnT;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;

@Schema(description = "基础游标型同步任务")
@Slf4j
public abstract class BaseExecuteJob implements ITestService {
    protected final SqlExecuteComponents components;
    protected final CursorSyncExecutor cursorSyncExecutor;
    protected final RedissonLockExecutor distributedLockExecutor;

    @SuppressWarnings("rawtypes")
    protected final ICursorSyncHandler handler;

    public BaseExecuteJob(SqlExecuteComponents components, ICursorSyncHandler<?, ?> handler) {
        this.components = components;
        this.handler = handler;
        this.cursorSyncExecutor = components.getCursorSyncExecutor();
        this.distributedLockExecutor = components.getDistributedLockExecutor();
    }

    /**
     * 执行 Job
     */
    public void execute() {
        distributedLockExecutor.executeWithLock("lock:" + getClass().getSimpleName(), 60, () -> {
            // 单次执行
            //noinspection unchecked,rawtypes
            cursorSyncExecutor.executeOnce(handler, (Comparable) getEnd());
            return ReturnT.SUCCESS;
        }, ReturnT.FAIL);
    }

    protected Comparable<?> getEnd() {
        return null;
    }

    @Override
    public void test() {
        execute();
    }
}
